package at.fhj.pse.pageobjects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class HoursPerActivityPage{
    private WebDriver driver;
    
    /*
     * Constructor
     */
    public HoursPerActivityPage(WebDriver driver){
        this.driver = driver;
    }
    

    /*
     * Input field: username:String
     */
    private String username;
    public String getUsername(){
        return username;
    }
    public void setUsername(String username){
        this.username = username;
    }
    
    
    /*
     * Input field: password:String
     */
    private String password;
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    

	/*
	 * Input field: acceptNextAlert:Boolean
	 */
	private Boolean acceptNextAlert;
	public Boolean getacceptNextAlert(){
		return acceptNextAlert;
	}
	public void setacceptNextAlert(Boolean acceptNextAlert){
		this.acceptNextAlert = acceptNextAlert;
	}
	/*
	 * Input field: From
	 */
	private String from;
	public String getFrom() {
		
		return from;
	}
	public void setFrom(String string) {
		
		this.from = string;
	}
	/*
	 * Input field: to
	 */
	private String to;
	public String getTo() {
		
		return to;
	}
	public void setTo(String string) {
		
		this.to = string;
	}
	/*
	 * Action: ShowHoursPerActivity - Projektarbeit
	 */
	
	public String showHoursPerActivityProjektarbeit() throws InterruptedException{
		driver.findElement(By.id("j_idt35:month_label")).click();
		driver.findElement(By.xpath("//div[@id='j_idt35:month_panel']/div/ul/li[2]")).click();

		driver.findElement(By.id("j_idt35:intervalsFrom_input")).clear();
		driver.findElement(By.id("j_idt35:intervalsFrom_input")).sendKeys(getFrom());
		driver.findElement(By.id("j_idt35:intervalsTo_input")).clear();
		driver.findElement(By.id("j_idt35:intervalsTo_input")).sendKeys(getTo());
		driver.findElement(By.id("j_idt35:j_idt41")).click();
		
		
		String smsg = getSelectorMessage("25/11/2014", "tr.ui-widget-content.ui-datatable-even > td");
		assertEquals("25/11/2014", smsg);

		String xmsg = getPathMessage("10h 30min", "//tbody[@id='j_idt35:outHourList_data']/tr/td[2]");
		assertEquals("10h 30min", xmsg);

		String xmsg2 = getPathMessage("2h 0min", "//tbody[@id='j_idt35:outHourList_data']/tr/td[3]");
		assertEquals("2h 0min", xmsg2);

		String xmsg3 = getPathMessage("+ 8h 30min", "//tbody[@id='j_idt35:outHourList_data']/tr/td[4]");
		assertEquals("+ 8h 30min", xmsg3);



		String smsg2 = getSelectorMessage("26/11/2014", "tr.ui-widget-content.ui-datatable-odd > td");
		assertEquals("26/11/2014", smsg2);

		String xmsg4 = getPathMessage("21h 0min", "//tfoot[@id='j_idt35:outHourList_foot']/tr/td[2]");
		assertEquals("21h 0min", xmsg4);

		String xmsg5 = getPathMessage("4h 0min", "//tfoot[@id='j_idt35:outHourList_foot']/tr/td[3]");
		assertEquals("4h 0min", xmsg5);

		return getPathMessage("+ 17h 0min", "//tfoot[@id='j_idt35:outHourList_foot']/tr/td[4]");
	}
	
	/*
	 * Action:ShowHoursPerActivity - Forschungsarbeit
	 */
	public String showHoursPerActivityForschung() throws InterruptedException{
		driver.findElement(By.id("j_idt35:month_label")).click();
		driver.findElement(By.xpath("//div[@id='j_idt35:month_panel']/div/ul/li[3]")).click();
		driver.findElement(By.id("j_idt35:intervalsFrom_input")).clear();
		driver.findElement(By.id("j_idt35:intervalsFrom_input")).sendKeys(getFrom());
		driver.findElement(By.id("j_idt35:intervalsTo_input")).clear();
		driver.findElement(By.id("j_idt35:intervalsTo_input")).sendKeys(getTo());
		driver.findElement(By.id("j_idt35:j_idt41")).click();
		
		String smsg = getPathMessage("27/11/2014", "//tbody[@id='j_idt35:outHourList_data']/tr/td[1]");
		assertEquals("27/11/2014", smsg);
		
		String xmsg7 = getPathMessage("7h 30min", "//tbody[@id='j_idt35:outHourList_data']/tr/td[2]");
		assertEquals("7h 30min", xmsg7);

		String xmsg8 = getPathMessage("2h 0min", "//tbody[@id='j_idt35:outHourList_data']/tr/td[3]");
		assertEquals("2h 0min", xmsg8);

		return getPathMessage("+ 5h 30min", "//tbody[@id='j_idt35:outHourList_data']/tr/td[4]");
		
	}
	
	/*
	 * Action:ShowHoursPerActivity - Lehrveranstaltung
	 */
	public String showHoursPerActivityLehrveranstaltung() throws InterruptedException{
		driver.findElement(By.id("j_idt35:month_label")).click();
		driver.findElement(By.xpath("//div[@id='j_idt35:month_panel']/div/ul/li[4]")).click();
		driver.findElement(By.id("j_idt35:intervalsFrom_input")).clear();
		driver.findElement(By.id("j_idt35:intervalsFrom_input")).sendKeys(getFrom());
		driver.findElement(By.id("j_idt35:intervalsTo_input")).clear();
		driver.findElement(By.id("j_idt35:intervalsTo_input")).sendKeys(getTo());
		driver.findElement(By.id("j_idt35:j_idt41")).click();
		
		return getSelectorMessage("No records found.", "tr.ui-widget-content.ui-datatable-empty-message > td");
	}
	
	/*
	 * Action: SelectorMessage
	 */
	public String getSelectorMessage(String expected, String selector) throws InterruptedException{
		String cssSelector = driver.findElement(By.cssSelector(selector)).getText();
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if (expected.equals(cssSelector)) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}
		return cssSelector;
	}

	/*
	 * Action: PathMessage
	 */
	public String getPathMessage(String expected, String Path) throws InterruptedException{
		String xPath = driver.findElement(By.xpath(Path)).getText();
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if (expected.equals(driver.findElement(By.xpath(Path)).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}
		return xPath;
	}
}
